const express = require('express');
const multer = require('multer');
const DifyController = require('../controllers/dify.controller');
const authMiddleware = require('../middlewares/auth.middleware');

const router = express.Router();

// 配置 multer 用于处理文件上传
const upload = multer({
    storage: multer.memoryStorage(),
    limits: {
        fileSize: 5 * 1024 * 1024 // 限制 5MB
    },
    fileFilter: (req, file, cb) => {
        // 只允许图片
        if (file.mimetype.startsWith('image/')) {
            cb(null, true);
        } else {
            cb(new Error('只允许上传图片文件'));
        }
    }
}).fields([
    { name: 'file', maxCount: 1 },
    { name: 'sessionId' },
    { name: 'message' }
]);

// 创建会话
router.post('/sessions', authMiddleware, DifyController.createSession);

// 获取会话历史消息
router.get('/sessions/:sessionId/messages', authMiddleware, DifyController.getMessages);

// 发送消息（支持文件上传）
router.post('/messages', authMiddleware, upload, DifyController.sendMessage);

// 删除会话
router.delete('/sessions/:sessionId', authMiddleware, DifyController.deleteSession);

// 修改会话名
router.put('/sessions/:sessionId/rename', authMiddleware, DifyController.renameSession);

// 获取所有会话
router.get('/sessions', authMiddleware, DifyController.getSessions);

module.exports = router; 